package com.moco.launcher.fragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.app.AlarmManager;
import android.app.Fragment;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageButton;
import android.widget.TextView;

import com.moco.launcher.R;
import com.moco.launcher.alarm.Constant;
import com.moco.launcher.bean.PickItemInfo;
import com.moco.launcher.view.PickerView;
import com.moco.launcher.view.WheelView;

/**
 * 睡眠延时操作
 * 
 * @author leker
 *
 */
public class SleepDelayFragment extends Fragment implements OnClickListener {
	String TAG = "SleepDelayFragment";
	private View rootView;
	private Context mContext;
	private ImageButton sleep_main_exit_btn, sleep_main_confirm_btn;
	private ImageButton sleep_main_stub_time_btn, sleep_main_stub_power_btn;
	private TextView sleep_delay_time_txt, sleep_delay_power_txt;
	private ViewStub mainStub, minuteStub, powerStub;

	// private PickerView sleep_delay_powerlist;
	private WheelView sleep_delay_minutelist, sleep_delay_powerlist;
	private List<PickItemInfo> minuteData = new ArrayList<PickItemInfo>();
	private List<PickItemInfo> powerData = new ArrayList<PickItemInfo>();
	HashMap<String, Integer> minuteMap = new HashMap<String, Integer>();
	private String mMinuteSelectedKey;

	private String powerTxt, minuteTxt;

	public enum SHOW_TYPE {
		MAIN, MINUTE, POWER
	}

	private SHOW_TYPE currentType;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = getActivity();
		currentType = SHOW_TYPE.MAIN;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mContext = getActivity();
		rootView = inflater.inflate(R.layout.sleep_delay_main_lay, container,
				false);

		sleep_main_exit_btn = (ImageButton) rootView
				.findViewById(R.id.sleep_main_exit_btn);
		sleep_main_exit_btn.setOnClickListener(this);
		sleep_main_confirm_btn = (ImageButton) rootView
				.findViewById(R.id.sleep_main_confirm_btn);
		sleep_main_confirm_btn.setOnClickListener(this);

		mainStub = (ViewStub) rootView.findViewById(R.id.sleep_main_viewstub);
		mainStub.inflate();
		minuteStub = (ViewStub) rootView
				.findViewById(R.id.sleep_minute_viewstub);
		minuteStub.inflate();
		powerStub = (ViewStub) rootView.findViewById(R.id.sleep_power_viewstub);
		powerStub.inflate();

		sleep_main_stub_time_btn = (ImageButton) rootView
				.findViewById(R.id.sleep_main_stub_time_btn);
		sleep_main_stub_time_btn.setOnClickListener(this);
		sleep_delay_time_txt = (TextView) rootView
				.findViewById(R.id.sleep_delay_time_txt);
		sleep_delay_time_txt.setOnClickListener(this);
		sleep_main_stub_power_btn = (ImageButton) rootView
				.findViewById(R.id.sleep_main_stub_power_btn);
		sleep_main_stub_power_btn.setOnClickListener(this);
		sleep_delay_power_txt = (TextView) rootView
				.findViewById(R.id.sleep_delay_power_txt);
		sleep_delay_power_txt.setOnClickListener(this);

		loadViewStub(currentType);

		sleep_delay_minutelist = (WheelView) rootView
				.findViewById(R.id.sleep_delay_minutelist);
		initMinuteStub();
		sleep_delay_powerlist = (WheelView) rootView
				.findViewById(R.id.sleep_delay_powerlist);
		initPowerStub();
		return rootView;
		// return super.onCreateView(inflater, container, savedInstanceState);
	}

	private void initMinuteStub() {
		PickItemInfo item = new PickItemInfo();
		item.setText("15min");
		minuteMap.put("15min", 15);
		item.setId("0");
		minuteData.add(item);

		item = new PickItemInfo();
		item.setText("30min");
		minuteMap.put("30min", 30);
		item.setId("1");
		minuteData.add(item);

		item = new PickItemInfo();
		item.setText("45min");
		minuteMap.put("45min", 45);
		item.setId("2");
		minuteData.add(item);

		item = new PickItemInfo();
		item.setText("1hour");
		minuteMap.put("1hour", 60);
		item.setId("3");
		minuteData.add(item);

		item = new PickItemInfo();
		minuteMap.put("2hours", 120);
		item.setText("2hours");
		item.setId("4");
		minuteData.add(item);

		sleep_delay_minutelist.setData(minuteData);
		sleep_delay_minutelist
				.setOnSelectListener(new com.moco.launcher.view.WheelView.onSelectListener() {
					@Override
					public boolean onRelayout(String selectId,
							com.moco.launcher.view.WheelView.UiParam param) {
						Log.i(TAG, "onRelayout selectId is:" + selectId);
						param.HlColor = 0xffee7b1d;
						/*
						 * minuteTxt =
						 * minuteData.get(Integer.parseInt(selectId)).getText();
						 * sleep_delay_time_txt.setText(minuteTxt);
						 */
						return true;
					}

					@Override
					public boolean onStop(String nSelectId) {
						Log.i(TAG, "onStop nSelectId is:" + nSelectId);
						// mMinuteSelectedKey = nSelectId;
						for (PickItemInfo info : minuteData) {
							Log.i(TAG, "info.getId() is:" + info.getId()
									+ " info.getText() is:" + info.getText());
							if (info.getId().equals(nSelectId)) {
								minuteTxt = info.getText();
								sleep_delay_time_txt.setText(minuteTxt);
								break;
							}
						}
						return false;
					}

					@Override
					public boolean onSelect(String nSelectId) {
						Log.i(TAG, "onSelect nSelectId is:" + nSelectId);
						return false;
					}
				});
	}

	private void initPowerStub() {
		PickItemInfo item = new PickItemInfo();
		item.setText("暂停播放");
		item.setId("0");
		powerData.add(item);

		item = new PickItemInfo();
		item.setText("关机");
		item.setId("1");
		powerData.add(item);

		item = new PickItemInfo();
		item.setText("待机");
		item.setId("2");
		powerData.add(item);
		sleep_delay_powerlist.setData(powerData);
		sleep_delay_powerlist
				.setOnSelectListener(new com.moco.launcher.view.WheelView.onSelectListener() {
					@Override
					public boolean onRelayout(String selectId,
							com.moco.launcher.view.WheelView.UiParam param) {
						Log.d(TAG, "onSelected " + selectId);
						param.HlColor = 0xffee7b1d;
						return true;
					}

					@Override
					public boolean onStop(String nSelectId) {
						for (PickItemInfo info : powerData) {
							if (info.getId().equals(nSelectId)) {
								powerTxt = info.getText();
								sleep_delay_power_txt.setText(powerTxt);
								break;
							}
						}
						return false;
					}

					@Override
					public boolean onSelect(String nSelectId) {
						return false;
					}
				});
	}

	private void loadViewStub(SHOW_TYPE type) {
		mainStub.setVisibility(View.GONE);
		minuteStub.setVisibility(View.GONE);
		powerStub.setVisibility(View.GONE);

		switch (type) {
		case MAIN:
			mainStub.setVisibility(View.VISIBLE);
			break;
		case MINUTE:
			minuteStub.setVisibility(View.VISIBLE);
			// lowerStub.inflate();
			break;
		case POWER:
			powerStub.setVisibility(View.VISIBLE);
			break;
		}
		currentType = type;
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.sleep_main_exit_btn:
			// getActivity().onBackPressed();
			handleExit(currentType);
			break;
		case R.id.sleep_main_confirm_btn:
			handleConfirm(currentType);
			break;
		case R.id.sleep_main_stub_time_btn:
		case R.id.sleep_delay_time_txt:
			loadViewStub(SHOW_TYPE.MINUTE);
			break;
		case R.id.sleep_main_stub_power_btn:
		case R.id.sleep_delay_power_txt:
			loadViewStub(SHOW_TYPE.POWER);
			break;
		}
	}

	private void handleExit(SHOW_TYPE type) {
		switch (type) {
		case MAIN:
			getActivity().onBackPressed();
			break;
		case MINUTE:
		case POWER:
			loadViewStub(SHOW_TYPE.MAIN);
			break;
		}
	}

	/**
	 * 对不同情况处理
	 * 
	 * @param type
	 */
	private void handleConfirm(SHOW_TYPE type) {
		switch (type) {
		case MAIN:
			handleMinuteConfirm();
			getActivity().onBackPressed();
			break;
		case MINUTE:
			// handleMinuteConfirm();
		case POWER:
			loadViewStub(SHOW_TYPE.MAIN);
			break;
		}
	}

	/**
	 * 注册一个闹钟
	 */
	private void handleMinuteConfirm() {
		Log.i(TAG, "minuteMap is:" + minuteMap);
		Log.i(TAG, "minuteMap.get(sleep_delay_time_txt.getText()) is:"
				+ minuteMap.get(sleep_delay_time_txt.getText()));
		int hour = minuteMap.get(sleep_delay_time_txt.getText()) / 60;
		int minute = minuteMap.get(sleep_delay_time_txt.getText()) % 60;
		sendActionListener(hour, minute);
	}

	private void sendActionListener(int hour, int minute) {
		AlarmManager alarmManager = (AlarmManager) getActivity()
				.getSystemService(Context.ALARM_SERVICE);
		Calendar calendar = Calendar.getInstance(Locale.getDefault());
		calendar.setTimeInMillis(System.currentTimeMillis());
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		Intent intent = new Intent();
		intent.setAction("com.sleep.delay.handle.action");
		intent.putExtra(Constant.SLEEPDELAY_KEY,
				sleep_delay_power_txt.getText());
		PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(),
				0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
				calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
				pendingIntent);
	}
}
